package xyz.yuzegod.megawalls.classes.normal.blaze;

import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.CustomBlaze;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.EntityTypes;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SecondSkill extends Skill
{
    public static Map<GamePlayer, List<CustomBlaze>> blazes;
    
    public SecondSkill(final Classes classes) {
        super("烈焰人召唤术", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.5;
            }
            case 2: {
                return 0.75;
            }
            case 3: {
                return 1.0;
            }
            default: {
                return 0.5;
            }
        }
    }
    
    public int getAmount(final int level) {
        switch (level) {
            case 1: {
                return 1;
            }
            case 2: {
                return 1;
            }
            case 3: {
                return 2;
            }
            default: {
                return 1;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7击杀敌人时有§a" + StringUtils.percent(this.getAttribute(level)) + "§7几率");
            lore.add("   §7生成§a" + this.getAmount(level) + "个烈焰人。");
            return lore;
        }
        lore.add(" §8▪ §7击杀敌人时有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7几率");
        lore.add("   §7生成§8" + ((this.getAmount(level - 1) == this.getAmount(level)) ? Integer.valueOf(this.getAmount(level)) : (this.getAmount(level - 1) + " ➜ §a" + this.getAmount(level))) + "§7个烈焰人。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill2Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill2Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (MegaWalls.getRandom().nextInt(100) <= this.getAttribute(kitStats.getSkill2Level()) * 100.0) {
            final List<CustomBlaze> list = SecondSkill.blazes.getOrDefault(gamePlayer, new ArrayList<CustomBlaze>());
            for (int i = 1; i <= this.getAmount(this.getPlayerLevel(gamePlayer)); ++i) {
                final CustomBlaze blaze = new CustomBlaze((World)((CraftWorld)Bukkit.getWorld("world")).getHandle());
                blaze.setGamePlayer(gamePlayer);
                blaze.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
                EntityTypes.spawnEntity((Entity)blaze, gamePlayer.getPlayer().getLocation());
                list.add(blaze);
                SecondSkill.blazes.put(gamePlayer, list);
                Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
                    @Override
                    public void run() {
                        blaze.die();
                    }
                }, 1200L);
            }
        }
        return true;
    }
    
    static {
        SecondSkill.blazes = new HashMap<GamePlayer, List<CustomBlaze>>();
    }
}
